#/usr/bin/python
# coding: utf8
from lxml import etree
import urllib2
import sys
from urllib2 import URLError


def gethtml(url):
    '''
    Fetch the target html
    '''
    try:
        response = urllib2.urlopen(url)
        result = response.read()
        return  result
    except URLError, e:
        if hasattr(e.reason):
            print 'Failed to reach a server.'
            print 'Reason:', e.reason
            return None
        elif hasattr(e.code):
            print 'The server couldnt fulfill the request.'
            print 'Error code:', e.code
            return None

# end def gethtml

def getLits(taobaoid):
    '''
    根据http://api.taobao.com/apidoc/api.htm?path=cid:4-apiId:18
    获得卖家的所有商品title
    分词存入list
    '''
    pass


def getPaiming(word, itemid, mtaobaoid):
    '''
    搜索关键词，找到买家mtaobaoid的item，记录页数和排名
    '''
    pass


def decodeHtmlEntity(s):
    if s=='' or not s:
        return
    result = s
    import locale
    L1 = locale.getdefaultlocale()[1]
    result = result.decode(L1, "ignore").encode(L1).replace("xc2xa0", " ")
    return result
# end def decodeHtmlEntity

html = gethtml('http://menupalace.com')
if(html=='' or html==None):
    print "Cant get them html from http://menupalace.com"
    sys.exit()

try:
    tree = etree.HTML(html)
    nodes = tree.xpath("//table[@class=n_table]")
except:
    f = open("log.txt", "wa")
    f.write(html)
    print("error to resolve the html http://menupalace.com")
    sys.exit()

for node in nodes:
    n = node.xpath("./tr[1]/td[1]/img")
    # Fetch country
    country = ""
    if len(n)>0:
        country = decodeHtmlEntity(n[0].tail)
        country = country.strip()

    # Fetch all link
    ls = node.xpath(".//a")

    # Through all link
    for l in ls:
        #city
        city = decodeHtmlEntity(l.text)
        city = city.strip()

        prelink = l.get("href")
        link = prelink + "restaurants/restaurants.aspx"

        #print Fetching html from + link + ...
        html = gethtml(link)
        if(html=='' or html == None):
            print "Cant get them html from " + link
            continue

        try:
            subtree = etree.HTML(html)
            subnodes = subtree.xpath("//td[@class=frame_style_padding]")
        except:
            print 'error'

'''
做一个基于xpath来寻找指定元素的功能
满足搜索指定url，获得返回结果
在结果中搜索指定文本并获得位置
返回并记录位置
'''
gethtml()